package com.apes.auto.autoparts.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EpcPartNumberParse.CONST_TABLE_NAME)
public class EpcPartNumberParse extends Model {

    public static final String CONST_TABLE_NAME = "epc_partnumber_parse";


    @Id
    @Comment("编码")
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "EPC配件", padding = 8)
    private String id;


    @Comment("EPC版本")
    @Column(name = "epc_version", length = 120, nullable = false)
    private String epcVersion;

    @Comment("配件名称")
    @Column(name = "part_chinese_name", length = 120, nullable = false)
    private String partChineseName;


    @Comment("目录名称")
    @Column(name = "menu_name", length = 120, nullable = false)
    private String menuName;


    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "delete", name = "删除"),
            @State(id = "transApprove", name = "转审核"),
            @State(id = "approve", name = "审核"),
    })
    @Column(name = "state", length = 16, nullable = false)
    private String state;


    @Comment("转审核人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_approve_id", referencedColumnName = "id")
    private PartyRole transferApproveId;


    @Comment("转审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "transfer_approve_date")
    private Date transferApproveDate;


    @Comment("审核人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_id", referencedColumnName = "id")
    private PartyRole approveId;


    @Comment("审核日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    private Date approveDate;



    @Transient
    private int parseNum;

    public int getParseNum() {
        return this.epcPartNumberParseList == null ? 0 : this.epcPartNumberParseList.size();
    }



    @OneToMany(mappedBy = "epcPartNumberParse", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("imgOrder,svn,id ASC")
    private List<EpcPartNumberParseList> epcPartNumberParseList = new ArrayList();

    public List<EpcPartNumberParseList> getEpcPartNumberParseList() {
        if( epcPartNumberParseList == null ) return new ArrayList();
        for (EpcPartNumberParseList item : epcPartNumberParseList) {
            item.setEpcPartNumberParse(this);
        }
        return epcPartNumberParseList;
    }


    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

}
